<template>
  <div class="app-container">
    <!-- 操作 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" @submit.prevent>
      <el-form-item prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入IP"
          Query
          clearable
          style="width: 240px"
          @keyup.enter="searchQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="searchQuery"
          >搜索</el-button
        >
        <el-tooltip
          class="item"
          effect="dark"
          content="查询局域网内所有可用IP,将会耗时3-5分钟"
          placement="top"
        >
          <el-button
            icon="Refresh"
            @click="refreshQuery"
            :disabled="refreshDisabled"
            >强制刷新</el-button
          >
        </el-tooltip>
        <el-button type="primary" plain icon="Plus" @click="tableConnect"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 表格数据 -->
    <el-table
      v-loading="loading"
      :data="hostList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="80" align="center" />
      <el-table-column label="序号" width="150">
        <template #default="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column label="系统" prop="osName" width="150" />
      <el-table-column label="IP" width="200">
        <template #default="scope">
          <span v-if="scope.row.isOnline != '0'">{{ scope.row.ip }}</span>
          <el-tooltip
            placement="top"
            :content="scope.row.disconnectReason"
            v-if="scope.row.isOnline == '0'"
          >
            <span class="scope_proc_red">{{ scope.row.ip }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" width="500" />
      <el-table-column label="操作" align="center" width="500">
        <template #default="scope">
          <el-button
            @click="tableConnect(scope.row)"
            :class="scope.row.status === 1 ? 'portrait_btn' : 'system_btn'"
            >{{ scope.row.status === 1 ? "断开" : "连接" }}
          </el-button>
          <el-button
            @click="tableInstall(scope.row)"
            :class="scope.row.isInstall === 1 ? 'portrait_btn' : 'system_btn'"
            >{{ scope.row.isInstall === 1 ? "卸载" : "安装" }}
          </el-button>
          <el-button @click="tableRemark(scope.row)">备注</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getData"
    />
    <!-- 编辑备注弹框 -->
    <el-dialog
      :title="remark_title"
      v-model="remarkStatus"
      width="500px"
      append-to-body
    >
      <el-input
        type="textarea"
        v-model="remarkParams.remark"
        placeholder="请输入备注"
        class="remark-text"
      ></el-input>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submit">保 存</el-button>
          <el-button @click="cancel">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 连接主机弹框 -->
    <el-dialog
      title="主机连接"
      v-model="openStatus"
      width="500px"
      append-to-body
    >
      <el-form ref="roleRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="系统" prop="osName">
          <el-select v-model="form.osName" placeholder="请选择系统">
            <el-option label="Linux" value="Linux" />
          </el-select>
        </el-form-item>
        <el-form-item label="IP地址" prop="ip">
          <el-input v-model="form.ip" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            auto-complete="off"
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="form.password"
            placeholder="请输入密码"
            maxlength="20"
            auto-complete="off"
            show-password
          />
        </el-form-item>
        <el-form-item label="端口号" prop="port">
          <el-input
            v-model="form.port"
            placeholder="请输入端口号"
            auto-complete="off"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :loading="but_loading" @click="connect"
            >连 接</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  getAllList,
  getListRefresh,
  setRemark,
  connectHost,
  disconnectHost,
  getHostListByIp,
  installHost,
  unloadHost,
} from "@/api/system/host";
import { nextTick, ref } from "vue";
import { ElLoading } from "element-plus";

const { proxy } = getCurrentInstance();
const loading = ref(true);
const but_loading = ref(false);
const hostList = ref([]);
const ids = ref([]);
const total = ref(0);
const remark_title = ref("");
const remarkStatus = ref(false);
const openStatus = ref(false);
const refreshDisabled = ref(false);
const data = reactive({
  form: {
    osName: "",
    ip: "",
    username: "",
    password: "",
    port: "22",
  },
  rules: {
    osName: [{ required: true, message: "系统不能为空", trigger: "blur" }],
    ip: [{ required: true, message: "IP地址不能为空", trigger: "blur" }],
    username: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
    password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
    port: [{ required: true, message: "端口号不能为空", trigger: "blur" }],
  },
  queryParams: {
    ip: "",
    pageNum: 1,
    pageSize: 10,
  },
  remarkParams: {
    ip: "",
    remark: "",
  },
});
const { form, rules, queryParams, remarkParams } = toRefs(data);

/** 查询列表数据 */
function getData() {
  loading.value = true;
  refreshDisabled.value = true;
  if (queryParams.value.ip) {
    getHostListByIp(queryParams.value).then((response) => {
      if (response.data.result === true) {
        hostList.value = response.data.data;
        total.value = response.data.total;
        loading.value = false;
        refreshDisabled.value = false;
      } else if (
        response.data.result === false &&
        response.data.info === "强制刷新正在进行！"
      ) {
        proxy.$message({
          type: "warning",
          message: response.data.info,
        });
      } else {
        loading.value = false;
        refreshDisabled.value = false;
        proxy.$message({
          type: "error",
          message: response.data.info,
        });
      }
    });
  } else {
    getAllList(queryParams.value).then((response) => {
      if (response.data.result === true) {
        hostList.value = response.data.data;
        total.value = response.data.total;
        loading.value = false;
        refreshDisabled.value = false;
      } else if (
        response.data.result === false &&
        response.data.info === "强制刷新正在进行！"
      ) {
        proxy.$message({
          type: "warning",
          message: response.data.info,
        });
      } else {
        loading.value = false;
        refreshDisabled.value = false;
        proxy.$message({
          type: "error",
          message: response.data.info,
        });
      }
    });
  }
}

/** 强制刷新操作 */
function refreshQuery() {
  loading.value = true;
  refreshDisabled.value = true;
  queryParams.value.ip = "";
  debugger;
  getListRefresh(queryParams.value).then((response) => {
    if (response.data.result === true) {
      hostList.value = response.data.data;
      total.value = response.data.total;
      loading.value = false;
      refreshDisabled.value = false;
    } else {
      loading.value = false;
      refreshDisabled.value = false;
      proxy.$message({
        type: "error",
        message: response.data.info,
      });
    }
  });
}

/** 搜索按钮操作 */
function searchQuery() {
  queryParams.value.pageNum = 1;
  getData();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
}

/** 操作-备注 */
function tableRemark(row) {
  remarkStatus.value = true;
  remarkParams.value.ip = row.ip;
  if (row.remark) {
    remark_title.value = "修改备注";
    remarkParams.value.remark = row.remark;
  } else {
    remark_title.value = "添加备注";
    remarkParams.value.remark = "";
  }
}

/** 操作-备注-弹窗保存 */
function submit() {
  // if (!remarkParams.value.remark) {
  //   proxy.$modal.msgError("备注不能为空");
  //   return;
  // }
  setRemark(remarkParams.value).then((response) => {
    if (response.data === true) {
      proxy.$modal.msgSuccess("保存成功");
      remarkStatus.value = false;
      getData();
    } else {
      remarkStatus.value = false;
      proxy.$message({
        type: "error",
        message: "操作失败",
      });
    }
  });
}

/** 操作-连接 */
function tableConnect(row) {
  if (row.status === 1) {
    proxy
      .$confirm("此操作将断开主机连接, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(() => {
        const params = {
          ip: row.ip,
        };
        loading.value = true;
        disconnectHost(params).then((response) => {
          if (response.data.result === true) {
            loading.value = false;
            proxy.$message({
              type: "success",
              message: "操作成功!",
            });
            getData();
          } else {
            loading.value = false;
            proxy.$message({
              type: "error",
              message: response.data.info,
            });
          }
        });
      })
      .catch(() => {
        proxy.$message({
          type: "info",
          message: "已取消断开连接",
        });
      });
  } else {
    form.value.ip = row.ip;
    openStatus.value = true;
  }
}

/** 操作-连接-弹窗连接 */
function connect() {
  but_loading.value = true;
  connectHost(form.value).then((response) => {
    if (response.data.result === true) {
      but_loading.value = false;
      proxy.$modal.msgSuccess("连接成功");
      openStatus.value = false;
      getData();
      proxy.resetForm("roleRef");
    } else {
      but_loading.value = false;
      proxy.$message({
        type: "error",
        message: response.data.info,
      });
      openStatus.value = false;
      proxy.resetForm("roleRef");
    }
  });
}

/** 操作-安装卸载 */
function tableInstall(row) {
  if (row.isInstall === 1) {
    proxy
      .$confirm("此操作将卸载故障注入安装包, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(() => {
        const params = {
          ip: row.ip,
        };
        loading.value = true;
        unloadHost(params).then((response) => {
          if (response.data.result === true) {
            loading.value = false;
            proxy.$message({
              type: "success",
              message: "操作成功!",
            });
            getData();
          } else {
            loading.value = false;
            proxy.$message({
              type: "error",
              message: response.data.info,
            });
          }
        });
      })
      .catch(() => {
        proxy.$message({
          type: "info",
          message: "已取消",
        });
      });
  } else {
    const params = {
      ip: row.ip,
    };
    loading.value = true;
    installHost(params).then((response) => {
      if (response.data.result === true) {
        loading.value = false;
        proxy.$message({
          type: "success",
          message: "操作成功!",
        });
        getData();
      } else {
        loading.value = false;
        proxy.$message({
          type: "error",
          message: response.data.info,
        });
      }
    });
  }
}

/** 弹窗-关闭 */
function cancel() {
  remarkStatus.value = false;
  openStatus.value = false;
}
getData();
</script>

<style>
.scope_percent {
  font-weight: bold;
  background: #409eff;
  padding: 3px 6px;
  color: #fff;
  font-size: 12px;
  border-radius: 3px;
}
.system_btn,
.chart_btn,
.portrait_btn {
  background: #409eff;
  width: 60px;
  height: 32px;
  text-align: center;
  line-height: 32px;
  color: #fff;
  font-size: 14px;
  border-radius: 4px;
  border: 0;
}
.system_btn:hover,
.chart_btn:hover {
  background: rgba(64, 158, 255, 0.8);
  color: #fff;
}
.portrait_btn {
  background: #e15361 !important;
}
.portrait_btn:hover {
  background: rgba(225, 83, 97, 0.8) !important;
  color: #fff;
}
.remark-text textarea {
  height: 70px;
}
.scope_proc_red {
  font-weight: bold;
  background: #e15361;
  padding: 3px 6px;
  color: #fff;
  font-size: 12px;
  border-radius: 3px;
}
</style>
